package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IStrateCommentService;
import cn.wolfcode.trip.base.service.IStrategyCommentLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/strategyComments")
public class StrategyCommentController {
    @Autowired
    private IStrateCommentService strateCommentService;
    @Autowired
    private IStrategyCommentLikeService strategyCommentLikeService;

    // 发送ajax请求获取所有骡友对当前攻略点评(审核通过)
    /**
     * 接口分析:
     * 1.确定资源: /strategyComments/{strategyId}/1}
     * 2.请求方式:GET
     * 3.请求参数:state
     * 4.返回结果:List<strategyComment> strategyComments JSON
     */
    @GetMapping("/{strategyId}/{state}")
    public Object queryStrategyCommentsByStrategyIdAndState(@PathVariable("strategyId") Long strategyId,@PathVariable("state") Integer state){
        List<StrategyComment> list = strateCommentService.queryStrategyCommentsByStrategyIdAndState(strategyId,state);
        return list;
    }

    // 获取指定id的StrategyComment
    /**
     * 1.资源:/strategyComments/{id}
     * 2.请求方式:GET
     * 3.请求参数:
     * 4.返回结果: List<StrategyComment> JSON
     */
    @GetMapping("/{id}")
    public Object getStrategyCommentById(@PathVariable("id") Long id){
        Map<String,Object> map = new HashMap<String, Object>();
        StrategyComment strategyComment = strateCommentService.getStrategyCommentById(id);
        String imgUrls = strategyComment.getImgUrls();
        String[] imgUrlsArr = imgUrls.split(";");
        map.put("strategyComment",strategyComment);
        map.put("imgUrlsArr", Arrays.asList(imgUrlsArr));
        return map;
    }

    // ====================刘涛==================================
    // 攻略点赞开发判断当前用户是否登录
    @GetMapping("/isLogin")
    public Object isLogin(){
        Map<String,Object> map = new HashMap<String, Object>(); // 用map来封装需求数据
        User currentUser = UserContext.getCurrentUser();
        if (currentUser == null){
            map.put("state",0);
        }else{
            map.put("state",1);
        }
        return map;
    }
    // 攻略点赞
    @PostMapping("like")
    public Object getCountAndState(Long strategyCommentId){
        // 获取系统当前登录用户
        Long userId = UserContext.getCurrentUser().getId();
        // 获取点赞数(返回值包括state用于店点赞状态的显示)
        Long num = strategyCommentLikeService.checkNumByUserIdAndStrategyCommentId(userId,strategyCommentId);
        return num;
    }
    // 获取当前攻略点赞数
    @GetMapping("/{strategyCommentId}/num")
    public Object getNumByStrategyCommentId(@PathVariable Long strategyCommentId){
        return strategyCommentLikeService.getNumByStrategyId(strategyCommentId);
    }

    // =====================end==================================
}
